﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using DAL;
using System.Reflection;

namespace 包覆门
{
    public partial class DotonNoPriceView : Form
    {
        public DotonNoPriceView()
        {
            InitializeComponent();
        }
        public string Danhao { get; set; }
        public bool IsDoton { get; set; }
        private OrderDAL dal = new OrderDAL();//数据仓库
        private AccountDAL acDal = new AccountDAL();
        private DictionaryTypeDAL dicDal = new DictionaryTypeDAL();
        private void TestView_Load(object sender, EventArgs e)
        {
            foreach (RenderingExtension re in reportViewer1.LocalReport.ListRenderingExtensions())
            {
                //屏蔽掉你需要取消的导出功能 Excel PDF WORD  
                if (re.Name == "Excel")//屏蔽掉Excel PDF WORD类似
                {
                    FieldInfo fi = re.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
                    fi.SetValue(re, false);
                }
            }
            //查询主订单
            DataTable mainorder = dal.ReportMainOrderByDanhao(Danhao);
            //查询详细订单
            DataTable detail = dal.ReportDetailOrderByDanhao(Danhao);
            
            //门型订单详情
            DataTable menxingorder = dal.ReportMXOrderNoPriceByDanhao(Danhao);
            if (menxingorder.Rows.Count <= 0) {
                menxingorder.Rows.Add(menxingorder.NewRow());
            }

            //装饰线条详情
            DataTable zsxtorder = dal.ReportZSXTOrderNoPriceByDanhao(Danhao);
            if (zsxtorder.Rows.Count <= 0) {
                zsxtorder.Rows.Add(zsxtorder.NewRow());
            }
            
            //查询做旧价格价格字典
            DataTable dtColor = dicDal.ColorListAll();

            //查询玻璃
            DataTable dtGlass = dicDal.SpecialGlassListAll();

            DataTable acc = new DataTable();
            //查询打款账号
            if (IsDoton)
            {
                 acc = acDal.ListDotonAll();
            }
            else {
                 acc = acDal.ListAll();
            }

             detail.Columns.Add(new DataColumn("mianji1",typeof(System.Decimal),"Convert(mianji,System.Decimal)"));
             detail.Columns.Add(new DataColumn("total1", typeof(System.Decimal), "Convert(total,System.Decimal)"));
             detail.Columns.Add(new DataColumn("num1", typeof(System.Decimal), "Convert(num,System.Decimal)"));
             detail.Columns.Add(new DataColumn("gao1", typeof(System.Decimal), "Convert(gao,System.Decimal)"));
             detail.Columns.Add(new DataColumn("glassprice1", typeof(System.Decimal), "Convert(glassprice,System.Decimal)"));
             detail.Columns.Add(new DataColumn("oldprice1", typeof(System.Decimal), "Convert(oldprice,System.Decimal)"));
             detail.Columns.Add(new DataColumn("otherprice1", typeof(System.Decimal), "Convert(otherprice,System.Decimal)"));
             
             detail.Columns.Add(new DataColumn("zscd", typeof(System.Decimal), "gao1*num1"));

            string totalmianji = detail.Compute("sum(mianji1)", "isdel='0' and zslb='0' ").ToString();
            if (string.IsNullOrWhiteSpace(totalmianji)) totalmianji = "0";
             //string totalmianji = detail.Compute("sum('mianji')", "isdel='0' and zslb='0' ").ToString();
             string totalPrice = detail.Compute("sum(total1)","isdel='0' and zslb='0'").ToString();
             string totalnum = detail.Compute("sum(num1)","isdel=0 and zslb='0'" ).ToString();
             string zstotalPrice = detail.Compute("sum(total1)", "isdel='0' and zslb='1'").ToString();
             string zstotalnum = detail.Compute("sum(num1)", "isdel=0 and zslb='1'").ToString();
             string zstotalmeter = detail.Compute("sum(zscd)/1000", "isdel=0 and zslb='1' and iszt='0'").ToString();
             List<string> list = new List<string>();

             DataTable specialData = new DataTable();
             specialData.Columns.Add(new DataColumn("proname", typeof(string)));
             specialData.Columns.Add(new DataColumn("pronum", typeof(string)));
             specialData.Columns.Add(new DataColumn("proprice", typeof(string)));
             specialData.Columns.Add(new DataColumn("prototal", typeof(string)));
             specialData.Columns.Add(new DataColumn("prounit", typeof(string)));
           // DataView dv = 
             string menxingAdd = "";
            

             //芯板做旧填充内容
             DataRow dr1 = specialData.NewRow();
             dr1["proname"] = "芯板做旧";
             object o1 = SqlHelper.FromDbValue(detail.Compute("sum(mianji1)","isdel='0' and zslb='0' and isold='1'"));
           
            dr1["pronum"]=(o1==null?"0":o1.ToString());
            dr1["prounit"]="㎡";
            DataTable distinctColor = dal.GetReportColorDistint(Danhao);
            if (distinctColor.Rows.Count > 1)
            {
                MessageBox.Show("芯板做旧颜色不能存在两种以上，请确认");
                return;
            }
            else if (distinctColor.Rows.Count == 1)
            {
                DataRow[] drsColor = dtColor.Select("mid='" + distinctColor.Rows[0]["oldcolorid"] + "'");
                //dr1["proprice"] = drsColor[0]["mprice"].ToString();
                dr1["proname"] = drsColor[0]["mcolor"].ToString() + "芯板做旧";
                //specialData.Rows.Add(dr1);
            }
            else {
               // dr1["proprice"] = "0";
            }
            //string p1 = detail.Compute("sum(oldprice1)", "isdel='0' and zslb='0' and isold='1'").ToString();
            //list.Add(p1);
            //dr1["prototal"] = p1;
            specialData.Rows.Add(dr1);
            //玻璃附加费填充内容
            DataRow dr2 = specialData.NewRow();
            dr2["proname"] = "玻璃附加费";
            object o2 =detail.Compute("sum(mianji1)", "isdel='0' and zslb='0' and isglass='1'");
            dr2["pronum"] = (o2==null? "0":o2.ToString());
            dr2["prounit"] = "㎡";
            DataTable distinctGlass = dal.GetReportGlassDistint(Danhao);
            if (distinctGlass.Rows.Count > 1)
            {
                MessageBox.Show("玻璃芯板不能存在两种以上，请确认");
                return;
            }
            else if (distinctGlass.Rows.Count == 1)
            {
                DataRow[] drsGlass = dtGlass.Select("gid='" + distinctGlass.Rows[0]["glassid"] + "'");
                //dr2["proprice"] = drsGlass[0]["glassprice"].ToString();
                dr2["proname"] = drsGlass[0]["glasstype"].ToString() + "替换";
               // specialData.Rows.Add(dr1);
            }
            else
            {
                //dr2["proprice"] = "0";
            }
            //string p2 = detail.Compute("sum(glassprice1)", "isdel='0' and zslb='0' and isglass='1'").ToString();
            //list.Add(p2);
            //dr2["prototal"] = p2;
            specialData.Rows.Add(dr2);
            //装饰线条附加内容
            DataRow dr3 = specialData.NewRow();
            dr3["proname"] = "装饰线条做旧费";
            object zt = SqlHelper.FromDbValue(detail.Compute("sum(zscd)", "isdel='0' and zslb='1' and isold='1' and iszt='0'"));
            decimal zz = (zt==null?0:Convert.ToDecimal(zt));
            decimal zscd = Math.Round(zz/1000,3);
            dr3["pronum"] = zscd;
            dr3["prounit"] = "m";
            DataTable distinctZsxt=dal.GetReportZsxtColorDistint(Danhao);
            if(distinctZsxt.Rows.Count>1){
                MessageBox.Show("装饰线条做旧颜色不能存在两种以上，请确认");
                return;
            }else if(distinctZsxt.Rows.Count==1){
                DataRow[] drZsxt= dtColor.Select("mid='"+distinctZsxt.Rows[0]["oldcolorid"].ToString()+"'");
                //dr3["proprice"] = drZsxt[0]["mprice"].ToString();
                dr3["proname"] = drZsxt[0]["mcolor"].ToString()+"(柱身)";
            }else{
                //dr3["proprice"]="0";
            }
            //string p3 = detail.Compute("sum(oldprice1)", "isdel='0' and zslb='1' and isold='1' and iszt='0'").ToString();
            //list.Add(p3);
            //dr3["prototal"] = p3;
            specialData.Rows.Add(dr3);
            //柱头计算
            //DataRow dr6 = specialData.NewRow();
            //dr6["proname"] = "柱头做旧费";

            //dr6["pronum"] = detail.Compute("sum(num1)", "isdel='0' and zslb='1' and isold='1' and iszt='1'").ToString();
            //dr6["prounit"] = "个";
            //DataTable distinctZtzj = dal.GetReportZtzjColorDistint(Danhao);
            //if (distinctZtzj.Rows.Count > 1)
            //{
            //    MessageBox.Show("装饰线条做旧颜色不能存在两种以上，请确认");
            //    return;
            //}
            //else if (distinctZtzj.Rows.Count == 1)
            //{
            //    DataRow[] drZsxt = dtColor.Select("mid='" + distinctZsxt.Rows[0]["oldcolorid"].ToString() + "'");
            //    //dr6["proprice"] = drZsxt[0]["mprice"].ToString();
            //    dr6["proname"] = drZsxt[0]["mcolor"].ToString()+"(柱头)";
            //}
            //else
            //{
            //    dr6["proprice"] = "0";
            //}
            //string p6 = detail.Compute("sum(oldprice1)", "isdel='0' and zslb='1' and isold='1' and iszt='1' ").ToString();
            //list.Add(p6);
            //dr6["prototal"] = p6;
            //specialData.Rows.Add(dr6);



            //门型附加费
            DataRow dd = null;
            DataRow dr6 = null;
            DataRow dr5 =null;
            DataRow[] drspackage = null;
            DataRow drpackage = null;
            foreach (DataRow d in detail.Rows)
            {
                
                if (d["isspecial"].ToString().Equals("1"))
                {
                    menxingAdd = menxingAdd + d["specialname"].ToString() + "、";
                     dd = specialData.NewRow();
                    dd["proname"] = d["specialname"];
                    dd["pronum"] = d["num"];
                    dd["prounit"] = "个";
                    //string pp = d["specialtotal"].ToString();
                    //list.Add(pp);
                    //dd["proprice"] = d["specialprice"];
                    //dd["prototal"] = pp;
                    specialData.Rows.Add(dd);
                }
                if (d["zslb"].ToString().Equals("1") && d["iszt"].ToString().Equals("1")&&d["isold"].ToString().Equals("1"))
                {
                    //柱头计算
                    dr6 = specialData.NewRow();
                    dr6["proname"] = "柱头做旧费";

                    //dr6["pronum"] = detail.Compute("sum(num1)", "isdel='0' and zslb='1' and isold='1' and iszt='1'").ToString();
                    dr6["pronum"] = detail.Compute("sum(zscd)/1000", "isdel=0 and zslb='1' and iszt='1' and isold='1'").ToString();
                    dr6["prounit"] = "米";
                    DataTable distinctZtzj = dal.GetReportZtzjColorDistint(Danhao);
                    if (distinctZtzj.Rows.Count > 1)
                    {
                        MessageBox.Show("装饰线条做旧颜色不能存在两种以上，请确认");
                        return;
                    }
                    else if (distinctZtzj.Rows.Count == 1)
                    {
                        DataRow[] drZsxt = dtColor.Select("mid='" + distinctZtzj.Rows[0]["oldcolorid"].ToString() + "'");
                       // dr6["proprice"] = dal.GetZtPrice(d["mxbh"].ToString());

                        dr6["proname"] = drZsxt[0]["mcolor"].ToString() + "(柱头)";
                    }
                    else
                    {
                       // dr6["proprice"] = "0";
                    }
                    //string p6 = detail.Compute("sum(oldprice1)", "isdel='0' and zslb='1' and isold='1' and iszt='1' ").ToString();
                    ////list.Add(p6);
                   // dr6["prototal"] = p6;
                    specialData.Rows.Add(dr6);
                }
                //其他费用
                if (Convert.ToDecimal(d["otherprice1"])>0)
                {
                    dr5 = specialData.NewRow();
                    dr5["proname"] = d["otherSm"];
                    dr5["pronum"] = "1";
                    dr5["prounit"] = "备注";
                   // string p5 = d["otherprice1"].ToString();
                    //list.Add(p5);
                   // dr5["proprice"] = p5;
                   // dr5["prototal"] = p5;
                    specialData.Rows.Add(dr5);
                }
            }
            foreach (DataRow d in detail.Rows)
            {
                //包装费用
                if (d["zslb"].ToString().Equals("0"))
                {
                    if (!string.IsNullOrWhiteSpace(d["packagetotal"].ToString()) && Convert.ToDecimal(d["packagetotal"]) > 0)
                    {
                        drspackage = specialData.Select("proname='" + d["packagename"].ToString() + "'");
                        if (drspackage != null && drspackage.Length > 0)
                        {

                            //drspackage[0]["proname"] = d["packagename"];
                            drspackage[0]["pronum"] = Convert.ToDecimal(d["mianji"]) + Convert.ToDecimal(drspackage[0]["pronum"]);
                            // drspackage[0]["prounit"] = "元/㎡";
                            //string pp = d["packagetotal"].ToString();
                            //list.Add(pp);
                            //drspackage[0]["proprice"] = d["packageprice"];
                            //drspackage[0]["prototal"] = Convert.ToDecimal(d["packagetotal"]) + Convert.ToDecimal(drspackage[0]["prototal"]);

                        }
                        else
                        {
                            drpackage = specialData.NewRow();
                            drpackage["proname"] = d["packagename"];
                            drpackage["pronum"] = d["mianji"];
                            drpackage["prounit"] = "元/㎡";
                            //string pp = d["packagetotal"].ToString();
                            //list.Add(pp);
                            //drpackage["proprice"] = d["packageprice"];
                            //drpackage["prototal"] = pp;
                            specialData.Rows.Add(drpackage);
                        }
                    }
                }
            }


            //包装费用
            //包装费用
            if (!string.IsNullOrWhiteSpace(mainorder.Rows[0]["bzfy"].ToString()) && Convert.ToDecimal(mainorder.Rows[0]["bzfy"].ToString()) > 0)
            {
                DataRow dr4 = specialData.NewRow();
                dr4["proname"] = "单独木箱包装(个)";
                dr4["prounit"] = "包";
                DataTable dt = dicDal.GetPackageUnitPrice();
                string p4 = mainorder.Rows[0]["bzfy"].ToString();
                if (dt.Rows.Count > 0)
                {
                    DataRow du = dt.Rows[0];
                    dr4["pronum"] = Math.Round(Convert.ToDecimal(p4) / Convert.ToDecimal(du["unitprice"]));
                    //dr4["proprice"] = du["unitprice"];
                }
                //string p4 = mainorder.Rows[0]["bzfy"].ToString();
                //list.Add(p4);
                //dr4["proprice"] = p4;
                //dr4["prototal"] = p4;
                specialData.Rows.Add(dr4);
            }
            //拉手
            DataRow drmain = mainorder.Rows[0];
            if (!string.IsNullOrWhiteSpace(drmain["handerid"].ToString()))
            {
                DataRow dr7 = specialData.NewRow();
                dr7["proname"] = drmain["handername"];
                dr7["pronum"] = drmain["handernum"];
                dr7["prounit"] = "个";
                //string p7 =drmain["handertotal"].ToString();
                //list.Add(p7);
                //dr7["proprice"] = drmain["handerprice"];
               // dr7["prototal"] = p7;
                specialData.Rows.Add(dr7);
            }
            if (!string.IsNullOrWhiteSpace(drmain["hingeid"].ToString())) {
                DataRow dr8 = specialData.NewRow();
                dr8["proname"] = drmain["hingename"];
                dr8["pronum"] = drmain["hingenum"];
                dr8["prounit"] = "个";
                //string p8 = drmain["hingetotal"].ToString();
               // list.Add(p8);
               // dr8["proprice"] = drmain["hingeprice"];
                //dr8["prototal"] = p8;
                specialData.Rows.Add(dr8);
            }
            //其他费用
            //DataRow dr5 = specialData.NewRow();
            //dr5["proname"] = "其他费用";
            //dr5["pronum"] = "1";
            //dr5["prounit"] = "件";
            //string p5 = detail.Compute("sum(otherprice1)", "isdel='0' ").ToString();
            //list.Add(p5);
            //dr5["proprice"] = p5;
            //dr5["prototal"] = p5;
            //specialData.Rows.Add(dr5);
            //计算总费用
            decimal op= 0;
            //foreach (string s in list) {

            //    op += Convert.ToDecimal((string.IsNullOrWhiteSpace(s) ? "0" : s));
            //}
            //内置参数赋值 totalPrice orderprice totalnum totalmianji
            ReportParameter[] rps = new ReportParameter[15];
            if (IsDoton) { 
                rps[0] = new ReportParameter("reportName", "大唐宅配包覆平开门订单");
            }else{
                rps[0] = new ReportParameter("reportName", "（外包）包覆平开门订单");
            }
            if (mainorder.Rows.Count == 1)
            {
                DataRow dr = mainorder.Rows[0];
                rps[1] = new ReportParameter("sname", dr["dealername"].ToString());
                rps[2] = new ReportParameter("username", dr["username"].ToString());
                rps[3] = new ReportParameter("address", dr["address"].ToString() + "  " + (string.IsNullOrWhiteSpace(dr["sname"].ToString()) ? "" : "(" + dr["sname"].ToString() + ")"));
                rps[4] = new ReportParameter("phone", dr["phone"].ToString() + "  " + (string.IsNullOrWhiteSpace(dr["Contactname"].ToString()) ? "" : "(" + dr["Contactname"].ToString() + ")"));
                rps[5] = new ReportParameter("planOrder", dr["planOrder"].ToString());
                rps[6] = new ReportParameter("danhao", dr["danhao"].ToString());
                rps[7] = new ReportParameter("totalPrice", "--");
                rps[8] = new ReportParameter("totalMianji", string.IsNullOrWhiteSpace(totalmianji) ? "0" : totalmianji);
                rps[9] = new ReportParameter("totalNum", string.IsNullOrWhiteSpace(totalnum) ? "0" : totalnum);
                rps[10] = new ReportParameter("zsxtnum", string.IsNullOrWhiteSpace(zstotalnum) ? "0" : zstotalnum);
                rps[11] = new ReportParameter("zsxtprice","--");
                //string o = Math.Round(op + Convert.ToDecimal(string.IsNullOrWhiteSpace(totalPrice) ? "0" : totalPrice) + Convert.ToDecimal((string.IsNullOrWhiteSpace(zstotalPrice) ? "0" : zstotalPrice)), 0).ToString();
                rps[12] = new ReportParameter("orderPrice", "--");
                rps[13] = new ReportParameter("hingename", dr["hingename"].ToString());//hingename
                rps[14] = new ReportParameter("zstotalmeter", string.IsNullOrWhiteSpace(zstotalmeter) ? "0" : zstotalmeter);//hingename
            }
            else
            {
                MessageBox.Show("出现两个订单，请联系管理员处理");
                return;

            }
           
             reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\DotonReport.rdlc";
            
             //指定数据集,数据集名称后为表,不是DataSet类型的数据集
             this.reportViewer1.LocalReport.DataSources.Clear();
             this.reportViewer1.LocalReport.SetParameters(rps);
             this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", zsxtorder));
             this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet2", menxingorder));
             this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet3", specialData));
             this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet4", acc)); 
            //显示报表
             this.reportViewer1.RefreshReport();
        }
        //distinct 查询
        public static DataTable Distinct(DataTable dt, string[] filedNames)
        {
            DataView dv = dt.DefaultView;
            DataTable DistTable = dv.ToTable("Dist", true, filedNames);
            return DistTable;
        }
    }
}
